package com.app.server.impl;

import com.app.common.Result;
import com.app.mapper.DisasterHandleMapper;
import com.app.mapper.DisasterReportingMapper;
import com.app.mapper.RecordFileMapper;
import com.app.model.DisasterHandleModel;
import com.app.model.DisasterHandleModel;
import com.app.model.DisasterReportingModel;
import com.app.model.RecordFileModel;
import com.app.server.DisasterHandleService;
import com.app.utils.CommonHelp;
import com.app.utils.StringTool;
import com.app.vo.DisasterHandleVo;
import com.app.vo.DisasterHandleVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DisasterHandleServiceImpl extends ServiceImpl<DisasterHandleMapper, DisasterHandleModel> implements DisasterHandleService {
    @Autowired
    private DisasterHandleMapper disasterHandleMapper;

    @Autowired
    private DisasterReportingMapper disasterReportingMapper;

    @Autowired
    private RecordFileMapper recordFileMapper;

    @Override
    public DisasterHandleVo getDisasterHandleList(Integer page, Integer limit, Integer disasterId, String name) {
        Page<DisasterHandleModel> page1 = new Page<>(page, limit);
        QueryWrapper<DisasterHandleModel> qw = new QueryWrapper<>();
        if (StringTool.isNotEmpty(name)) {
            qw.like("name", name);
        }
        qw.eq("disaster_id", disasterId);
        qw.orderByDesc("id");
        Page<DisasterHandleModel> disasterHandlePage = disasterHandleMapper.selectPage(page1, qw);
        List<DisasterHandleModel> disasterHandleList = disasterHandlePage.getRecords();

        long count = disasterHandlePage.getTotal();
        DisasterHandleVo disasterHandleVo = new DisasterHandleVo();
        disasterHandleVo.setList(disasterHandleList);
        disasterHandleVo.setCount((int) count);
        return disasterHandleVo;
    }

    @Override
    public Result<String> addAndUpdateHandle(DisasterHandleModel disasterHandleModel) {
        DisasterHandleModel disasterHandle = disasterHandleMapper.selectById(disasterHandleModel.getId());
        if (disasterHandle != null) {
            disasterHandleModel.setUpdateTime(CommonHelp.getNowTimestamp());
            disasterHandleMapper.updateById(disasterHandleModel);
            return Result.success("修改成功");
        } else {
            QueryWrapper<DisasterHandleModel> qw = new QueryWrapper<>();
            qw.eq("disaster_id", disasterHandleModel.getDisasterId());
            DisasterHandleModel handle = disasterHandleMapper.selectOne(qw);
            if (handle != null) {
                return Result.success("已处理");
            }
            disasterHandleModel.setCreateTime(CommonHelp.getNowTimestamp());

            StringBuilder s1 = new StringBuilder();
            StringBuilder s2 = new StringBuilder();
            if (disasterHandleModel.getPic() != null && disasterHandleModel.getPic().length > 0) {
                String[] pic = disasterHandleModel.getPic();
                for (int i = 0; i < pic.length; i++) {
                    String[] split = pic[i].split("/");
                    int length = split.length;
                    RecordFileModel recordFileModel = new RecordFileModel();
                    recordFileModel.setName(split[length - 1]);
                    recordFileModel.setUrl(pic[i]);
                    recordFileModel.setFileType(1);
                    recordFileModel.setCreateTime(CommonHelp.getNowTimestamp());
                    recordFileMapper.insert(recordFileModel);
                    if (i == pic.length - 1) {
                        s1.append(recordFileModel.getId());
                    } else {
                        s1.append(recordFileModel.getId()).append(",");
                    }
                }
            }
            if (disasterHandleModel.getVideo() != null && disasterHandleModel.getVideo().length > 0) {
                String[] pic = disasterHandleModel.getVideo();
                for (int i = 0; i < pic.length; i++) {
                    String[] split = pic[i].split("/");
                    int length = split.length;
                    RecordFileModel recordFileModel = new RecordFileModel();
                    recordFileModel.setName(split[length - 1]);
                    recordFileModel.setUrl(pic[i]);
                    recordFileModel.setFileType(2);
                    recordFileModel.setCreateTime(CommonHelp.getNowTimestamp());
                    recordFileMapper.insert(recordFileModel);
                    if (i == pic.length - 1) {
                        s2.append(recordFileModel.getId());
                    } else {
                        s2.append(recordFileModel.getId()).append(",");
                    }
                }
            }
            if (s1.length() > 0 && s2.length() > 0) {
                disasterHandleModel.setFileId(s1 + "," + s2);
            }
            if (s1.length() > 0 && s2.length() == 0) {
                disasterHandleModel.setFileId(s1.toString());
            }
            if (s2.length() > 0 && s1.length() == 0) {
                disasterHandleModel.setFileId(s2.toString());
            }
            disasterHandleMapper.insert(disasterHandleModel);
        }
        return Result.success("添加成功");
    }
}
